/*
    <a class="add-param-fly layui-btn layui-btn-primary layui-btn-kj layui-btn-xs">添加字段</a>
    <a class="del-param-fly layui-btn layui-btn-primary layui-btn-kj layui-btn-xs">删除字段</a>
    <a class="submit-param-fly layui-btn layui-btn-primary layui-btn-kj layui-btn-xs">提交</a>
*/

define(['jquery'],function(){

    //点击添加按钮
    $('.add-param-fly').click(function(){
        $("#myTb").append("<tr style='height:30px' class='flyinputOnly'><td class='fkey'></td><td></td></tr>");
    });

    //点击某个td，改为update状态
    $("#myTb").on("click","td",function(){
        if(user_type==1){
            var thisval = $(this).find('input').val();
            if(thisval==undefined)  thisval = "";

            thisnum = $(this).find('input').length;
            if(thisnum==0)
                $(this).html("<input class='layui-input-update-info' type='text' value='"+thisval+"'>");


        }
    });

    //点击某一行，添加样式（为删除做准备）
    $("#myTb").on("click","tr",function(){
        $("#myTb tr").removeClass("selected");
        $(this).addClass("selected");
    })

    //删除某行
    $('.del-param-fly').click(function(){
        if(user_type==1)
            $("#myTb").find("tr.selected").remove()
    });

    //提交
    $('.submit-param-fly').click(function(){
        if(user_type==1)
            addinfo();
    });



    /*****************************以下为函数******************************/
    //init
    var GlobalID = GlobalTuCeng = "";

    //获取全部数据转为data
    function GetAllData(){
        var thisAllList = $('.flyinputOnly .fkey input');
        //批量获取值
        var valuetmp = "id=" + GlobalID + "&name=" + GlobalTuCeng;
        for(var i=0; i<thisAllList.length; i++){
            var tmp = thisAllList.eq(i).val() + "=" + thisAllList.eq(i).parents('.flyinputOnly').find('input:last').val();
            var valuetmp = valuetmp + "&" + tmp;
        }
        return valuetmp;
    }


    //查询
    function selectfind(innerID,innerTuCeng){
        //init
        GlobalID = innerID;
        GlobalTuCeng = innerTuCeng;
        data = "id=" + GlobalID + "&name=" + GlobalTuCeng;
        $("#myTb").html("");
        $('#tableControl').show();
        //
        $.ajax({
            type: "POST",
            dataType: "json",
            url: selectURl,
            data: data,
            success: function(msg){
                if(msg.status==true){
                    //program_start
                    if(!$("#myTb tr").length > 0){
                        $.each(msg.info, function(i,n) {
                            var tbDom = "";
                            if(user_type==1){
                                tbDom += "<tr class='flyinputOnly'>" +
                                    "<td class='fkey'><input class='layui-input-update-info' type='text' value='" + i + "'></td>" +
                                    "<td><input class='layui-input-update-info' type='text' value='" + n + "'></td>" +
                                    "</tr>";
                            }else{
                                tbDom += "<tr class='flyinputOnly'>" +
                                    "<td class='fkey'>" + i + "</td>" +
                                    "<td>" + n + "</td>" +
                                    "</tr>";
                            }
                            $("#myTb").append(tbDom);
                            $("#tableControl").addClass('pt10');
                        });

                    }
                }
                //$('.infoselectshow').show();
                //$('.infoselectshow .layui-colla-content').show();
            }
        });
    }

    //添加 或 修改 或 删除.
    function addinfo(){
        //init
        data = GetAllData();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: addURl,
            data: data,
            success: function(msg){
                if(msg.status==true){
                    //program_start
                    //alert(msg.info);
                }else{
                    //error
                    //alert(msg.error);
                }
            }
        });
    }

    //修改
    function updatinfo(){
        //init
        data = GetAllData();
        $.ajax({
            type: "POST",
            dataType: "json",
            url: udpateURl,
            data: data,
            success: function(msg){
                if(msg.status==true){
                    //program_start
                }else{
                    //error
                    alert(msg.error);
                }
            }
        });
    }


    //删除
    function deleteinfo(){
        //init
        data = "id=ObjectID&name=TuCeng";
        $.ajax({
            type: "POST",
            dataType: "json",
            url: deleteURl,
            data: data,
            success: function(msg){
                if(msg.status==true){
                    //program_start
                }else{
                    //error
                    alert(msg.error);
                }
            }
        });
    }

    return {selectfind: selectfind};

});